import { CreateCrudOptionsProps, CreateCrudOptionsRet, AddReq, DelReq, EditReq, dict, compute } from '@fast-crud/fast-crud';
import * as api from './info.api';
import { dictionary } from '/@/utils/dictionary';
import { successMessage } from '../../../utils/message';
import { auth } from '/@/utils/authFunction';
import { nextTick, computed } from 'vue';


console.log( dictionary('payment_status'));

/**
 *
 * @param crudExpose：index传递过来的示例
 * @param context：index传递过来的自定义参数
 * @returns
 */
export const createCrudOptions = function ({ crudExpose, context }: CreateCrudOptionsProps): CreateCrudOptionsRet {
	const pageRequest = async (query: any) => {
		return await api.infoListAPI(query);
	};
	const editRequest = async ({ form, row }: EditReq) => {
		form.id = row.id;
		return await api.infoEditAPI(form);
	};
	const delRequest = async ({ row }: DelReq) => {
		return await api.infoDelAPI(row.id);
	};
	const addRequest = async ({ form }: AddReq) => {
		return await api.infoAddAPI(form);
	};

	return {
		crudOptions: {
			request: {
				pageRequest,
				addRequest,
				editRequest,
				delRequest,
			},
			pagination: {
				show: true,
			},
			actionbar: {
				buttons: {
					add: {
						show: auth('drug:info:add'),
					},
				},
			},
			rowHandle: {
				//固定右侧
				fixed: 'right',
				width: computed(() => {
					if (auth('info:AuthorizedAdd') || auth('info:AuthorizedSearch')) {
						return 420;
					}
					return 320;
				}),
				buttons: {
					view: {
						show: false
					},
					edit: {
						show: auth('drug:info:edit'),
					},
					remove: {
						show: auth('drug:info:delete'),
					},
				},
			},
			form: {
				col: { span: 24 },
				labelWidth: '100px',
				wrapper: {
					is: 'el-dialog',
					width: '600px',
				},
			},
			columns: {
				_index: {
					title: '序号',
					form: { show: false },
					column: {
						type: 'index',
						align: 'center',
						width: '70px',
						columnSetDisabled: true, //禁止在列设置中选择
					},
				},
				id: {
					title: 'ID',
					column: { show: false },
					search: { show: false },
					form: { show: false },
				},
				customer_name: {
					title: '姓名',
					search: { show: true },
					column: {
						minWidth: 90,
					},
					form: {
						rules: [{ required: true, message: '顾客姓名必填' }],
						component: {
							placeholder: '请输入顾客姓名',
						},
					},
				},
				customer_phone: {
					title: '手机号',
					search: { show: true },
					column: {
						minWidth: 90,
					},
					form: {
						rules: [{ required: true, message: '手机号必填' }],
						component: {
							placeholder: '请输入手机号',
						},
					},
				},
				actual_amount: {
					title: '实付金额',
					search: { show: false },
					column: {
						minWidth: 120,
						columnSetDisabled: true,
					},
					form: {
						rules: [{ required: true, message: '实付金额必填' }],
						component: {
							placeholder: '输入实付金额',
						},
					},
				},
				payment_method: {
					title: '支付方式',
					search: { show: false },
					type: 'dict-select',
					column: {
						minWidth: 90,
					},
					form: {
						rules: [{ required: true, message: '支付方式必填' }],
						component: {
							placeholder: '输入支付方式',
						},
					},
					dict: dict({data: dictionary('payment_method'),}),
					valueBuilder(context) {
						const { row, key } = context;
						console.log(row[key]);
						return row[key];
					},
				},
				status: {
					title: '状态',
					search: { show: false },
					type: 'dict-select',
					column: {
						minWidth: 90,
					},
					dict: dict({data: dictionary('payment_status'),}),
					form: {
						show: false
					},
					valueBuilder(context) {
						const { row, key } = context;
						console.log(row[key]);
						return row[key];
					},
				},
				order_no: {
					title: '订单号',
					search: { show: false },
					type: 'number',
					column: {
						minWidth: 120,
					},
					form: {
						show: false
					},
				},
				discount_amount: {
					title: '优惠金额',
					search: { show: false },
					type: 'number',
					column: {
						minWidth: 90,
						sortable: 'custom',
					},
					form: {
						rules: [{ required: true, message: '库存必填' }],
						component: {
							placeholder: '请输入库存',
						},
					},
				},
				total_amount: {
					title: '总金额',
					search: { show: false },
					type: 'string',
					column: {
						minWidth: 90,
					},
					form: {
						show: false
					},
				},
				created_time: {
					title: '创建时间',
					search: { show: false },
					type: 'string',
					column: {
						minWidth: 120,
					},
					form: {
						show: false
					},
				},				
			},
		},
	};
};
